package com.rqf.ls.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rqf.common.base.ProjectInfor;
import com.rqf.ls.service.IRegisterOfLawyerService;
import com.rqf.ls.service.IRegisterService;
import com.rqf.ls.service.IManagementUserService;
import com.server.password.MyMD5;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("userMng")
public class ManagementUserController {
	
	@Autowired
	private IManagementUserService userManagementService;
	@Autowired
	private IRegisterService registerService;
	@Autowired
	private IRegisterOfLawyerService lawyerRegisterService;
	
	@RequestMapping("")
	public ModelAndView loginPage(){
		ModelAndView mv = new ModelAndView("manager/login");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("login")
	public void login(HttpServletRequest request, 
			HttpServletResponse response){
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");
		//验证管理员权限
		Boolean isSuccess = userManagementService.login(userName, password);
		if (isSuccess) {
			SessionUtil.setSession(request, "userName", userName);
			SessionUtil.setSession(request, "password", MyMD5.circularlyEncrypt(password, 3));
		}
		OutputUtil.returnSuccess(response, isSuccess);
		//OutputUtil.returnJsonp(request, response, isSuccess.toString());
	}
	
	@RequestMapping("page")
	public ModelAndView page(HttpServletRequest request){
		ModelAndView mv = null;
		// 验证管理员权限
		String userName = SessionUtil.getStringAttri(request, "userName");
		String password = SessionUtil.getStringAttri(request, "password");
		Boolean vertified = userManagementService.verifySession(userName,
				password);
		if (vertified) {
			mv = new ModelAndView("manager/managerIndex");
		}else {
			mv = new ModelAndView("manager/login");
		}
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("user")
	public ModelAndView user(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("manager/user/managerUser");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("userList/{type}/{inBlackList}")
	public ModelAndView userList(HttpServletRequest request,@PathVariable Integer type, 
			@PathVariable Integer inBlackList){
		ModelAndView mv = null;
		// 验证管理员权限
		String userName = SessionUtil.getStringAttri(request, "userName");
		String password = SessionUtil.getStringAttri(request, "password");
		Boolean vertified = userManagementService.verifySession(userName,
				password);
		if (vertified) {
			mv = new ModelAndView("manager/user/userList");
		}else {
			mv = new ModelAndView("errors/noAuth");
		}
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("type",type);
		mv.addObject("inBlackList",inBlackList);
		return mv;
	}
	

	 /**
	 * 获取用户列表
	 * @param request
	 * @param response
	 * @param type 0表示所有，1表示普通用户，2表示律师，3表示待审核律师
	 * @param inBlackList 0正常用户，1黑名单用户
	 * @author ServerZhang
	 * @date 2016年5月13日
	 */
	@RequestMapping("userData/{type}/{inBlackList}")
	public void userData(HttpServletRequest request, 
			HttpServletResponse response,@PathVariable Integer type, 
			@PathVariable Integer inBlackList){
		RequestInput requestInput = new RequestInput(request);
		List<Map<String, Object>> userList = null;
		Integer count = null;
		DataVector<Map<String, Object>> list1 = null;
		DataVector<Map<String, Object>> list2 = null;
		switch (type) {
		case 0://所有用户
			list1 = registerService.userList(requestInput, inBlackList);
			list2 = lawyerRegisterService.userList(requestInput, inBlackList);
			userList = list1.getList();
			userList.addAll(list2.getList());
			count = list1.getCount();
			count += list2.getCount();
			break;
		case 1://普通用户
			list1 = registerService.userList(requestInput, inBlackList);
			userList = list1.getList();
			count = list1.getCount();
			break;
		case 2://律师 
			list2 = lawyerRegisterService.userList(requestInput, inBlackList);
			userList = list2.getList();
			count = list2.getCount();
			break;
		case 3://待审核律师
			list2 = lawyerRegisterService.checkingList(requestInput);
			userList = list2.getList();
			count = list2.getCount();
			break;

		default:
			break;
		}
		OutputUtil.returnGridData(response, count, JSONSerielizer.toJson(userList, null, null));
	}
	@RequestMapping("lawyerDetail/{phoneNo}")
	public ModelAndView lawyerDetail(HttpServletRequest request,
			@PathVariable String phoneNo){
		ModelAndView mv = null;
		// 验证管理员权限
		String userName = SessionUtil.getStringAttri(request, "userName");
		String password = SessionUtil.getStringAttri(request, "password");
		Boolean vertified = userManagementService.verifySession(userName,
				password);
		if (vertified) {
			mv = new ModelAndView("manager/user/lawyerDetail");
		}else {
			mv = new ModelAndView("errors/noAuth");
		}
		Map<String, Object> map = lawyerRegisterService.read(phoneNo);
		mv.addAllObjects(map);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("phoneNo",phoneNo);
		return mv;
	}
	@RequestMapping("userDetail/{phoneNo}")
	public ModelAndView userDetail(HttpServletRequest request,
			@PathVariable String phoneNo){
		ModelAndView mv = null;
		// 验证管理员权限
		String userName = SessionUtil.getStringAttri(request, "userName");
		String password = SessionUtil.getStringAttri(request, "password");
		Boolean vertified = userManagementService.verifySession(userName,
				password);
		if (vertified) {
			mv = new ModelAndView("manager/user/userDetail");
		}else {
			mv = new ModelAndView("errors/noAuth");
		}
		Map<String, Object> map = registerService.read(phoneNo);
		mv.addAllObjects(map);
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("phoneNo",phoneNo);
		return mv;
	}
	public void lawyerMap(HttpServletRequest request,HttpServletResponse response,
			@PathVariable String phoneNo){
		Map<String, Object> map = lawyerRegisterService.read(phoneNo);
		
	}
	
	@RequestMapping("toblack/{type}")
	public void toblack(HttpServletRequest request, HttpServletResponse response, 
			@PathVariable Integer type){
		String phoneNos = request.getParameter("phoneNos");
		String state = request.getParameter("state");
		Integer inBlackList = Integer.valueOf(state);
		Boolean isSuccess = true;
		if (type.equals(1)) {
			isSuccess = registerService.pullToBlack(phoneNos, inBlackList);
		}else if (type.equals(2)) {
			isSuccess = lawyerRegisterService.pullToBlack(phoneNos, inBlackList);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	@RequestMapping("towhite/{type}")
	public void towhite(HttpServletRequest request, HttpServletResponse response, 
			@PathVariable Integer type){
		String phoneNos = request.getParameter("phoneNos");
		String state = request.getParameter("state");
		Integer inBlackList = Integer.valueOf(state);
		Boolean isSuccess = true;
		if (type.equals(1)) {
			isSuccess = registerService.pullToBlack(phoneNos, inBlackList);
		}else if (type.equals(2)||type.equals(3)) {
			isSuccess = lawyerRegisterService.pullToBlack(phoneNos, inBlackList);
		}
		OutputUtil.returnSuccess(response, isSuccess);
	}
	 /**
	 * 律师审核
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年5月17日
	 */
	@RequestMapping("checkConfirm")
	public void checkConfirm(HttpServletRequest request, HttpServletResponse response){
		String phoneNo = request.getParameter("phoneNo");
		String checked = request.getParameter("checked");
		boolean isSuccess = lawyerRegisterService.check(phoneNo, Integer.parseInt(checked));
		OutputUtil.returnSuccess(response, isSuccess);
	}
	
}
